/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package cn.carbswang.ohos.numberpickerview.library;

import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;
import ohos.os.ProcessManager;

/**
 * HandlerThread
 *
 * @since 2021-08-12
 */
public class HandlerThread extends Thread {
    int mPriority;
    EventRunner mLooper;
    private EventHandler mHandler;

    public HandlerThread(String name) {
        super(name);
        mPriority = 0;
    }

    /**
     * Constructs a HandlerThread.
     *
     * @param name name
     * @param priority The priority to run the thread at. The value supplied must be from
     * {@link Thread} and not from java.lang.Thread.
     */
    public HandlerThread(String name, int priority) {
        super(name);
        mPriority = priority;
    }

    /**
     * Call back method that can be explicitly overridden if needed to execute some
     * setup before Looper loops.
     */
    protected void onLooperPrepared() {
    }

    @Override
    public void run() {
        synchronized (this) {
            mLooper = EventRunner.create(false); // 手动托管模式
            notifyAll();
        }
        ProcessManager.setThreadPriority(mPriority);
        onLooperPrepared();
        mLooper.run();
    }

    /**
     * This method returns the Looper associated with this thread. If this thread not been started
     * or for any reason isAlive() returns false, this method will return null. If this thread
     * has been started, this method will block until the looper has been initialized.
     *
     * @return The looper.
     */
    public EventRunner getLooper() {
        if (!isAlive()) {
            return null;
        }

        // If the thread has been started, wait until the looper has been created.
        synchronized (this) {
            while (isAlive() && mLooper == null) {
                try {
                    wait();
                } catch (InterruptedException e) {
                }
            }
            return mLooper;
        }
    }

    /**
     * 获取EventHandler
     *
     * @return a shared {@link EventHandler} associated with this thread
     * @hide
     */
    public EventHandler getThreadHandler() {
        if (mHandler == null) {
            mHandler = new EventHandler(getLooper());
        }
        return mHandler;
    }

    /**
     * Quits the handler thread's looper.
     * <p>
     * Causes the handler thread's looper to terminate without processing any
     * more messages in the message queue.
     * </p><p>
     *
     * @return True if the looper looper has been asked to quit or false if the
     * thread had not yet started running.
     */
    public boolean quit() {
        EventRunner looper = getLooper();
        if (looper != null) {
            looper.stop();
            return true;
        }
        return false;
    }
}
